package kr.co.casually;

import java.util.ArrayList;

import kr.co.casually.adapter.MyConcertDbAdapter;
import kr.co.casually.adapter.SubjCodeArtListAdapter;
import kr.co.casually.dialog.AppCloseDialog;
import kr.co.casually.dialog.SendDialog;
import kr.co.casually.types.ConcertData;
import kr.co.casually.util.Utils;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Spinner;

public class MyConcertListActivity extends Activity {

	private ArrayList<Integer> mCultCode = null;
	private SubjCodeArtListAdapter mSubjCodeArtListAdapter;
	private ArrayList<ConcertData> myConcertDataArrayList;
	private ListView listView;
	private MyConcertDbAdapter myConcertDbAdapter;
	private EditText searchWord;
	private ImageView btnSearch;
	private ImageView btnSeachOnOff = null;
	private LinearLayout search_linear = null;
	int genrePosition;
	private boolean mCateSearchLinear_flag = false;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.my_concert_list_act);
		listView = (ListView) findViewById(R.id.mylist_listview);
		listView.setFastScrollEnabled(true);
		btnSeachOnOff = (ImageView) findViewById(R.id.mylist_search_imgbtn);
		search_linear = (LinearLayout) findViewById(R.id.mylist_search_linear);
		myConcertDataArrayList = new ArrayList<ConcertData>();
		mCultCode = new ArrayList<Integer>();
		listView.setOnItemLongClickListener(mItemLongClickListener);
		listView.setOnItemClickListener(listViewItemClick);

		searchWord = (EditText) findViewById(R.id.mylist_searchword);
		btnSearch = (ImageView) findViewById(R.id.mylist_search_btn);

		btnSearch.setOnClickListener(clickListener);
		btnSeachOnOff.setOnClickListener(clickListener);

		if (mCateSearchLinear_flag == false) {
			search_linear.setVisibility(View.GONE);
			mCateSearchLinear_flag = true;
		}

		Spinner genreSpinner = (Spinner) findViewById(R.id.mylist_genre_spinner);
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.genre_name, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		genreSpinner.setAdapter(adapter);

		genreSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
			public void onItemSelected(AdapterView<?> parent, View view,
					int position, long id) {
				genrePosition = position;
				loadGenreConcertList(position);
			}

			public void onNothingSelected(AdapterView<?> parent) {
			}
		});

	}

	// 리스트뷰 아이템 클릭
	OnItemClickListener listViewItemClick = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {

			Intent ArtInfo = new Intent(getApplicationContext(), ArtInfo.class);
			ArtInfo.putExtra("cultCode", mCultCode.get(position));
			startActivity(ArtInfo);

		}
	};

	OnClickListener clickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			switch (v.getId()) {
			case R.id.mylist_search_btn:
				loadGenreConcertList(genrePosition);
				break;

			case R.id.mylist_search_imgbtn:

				if (mCateSearchLinear_flag == false) {
					search_linear.setVisibility(View.GONE);
					mCateSearchLinear_flag = true;
				} else {
					search_linear.setVisibility(View.VISIBLE);
					mCateSearchLinear_flag = false;
				}

				break;
			}

		}
	};

	// 공연 리스트 불러오기
	private void loadGenreConcertList(int genre) {
		if (mCultCode.size() > 0) {
			mCultCode.clear();
		}

		myConcertDataArrayList.clear();
		myConcertDbAdapter = new MyConcertDbAdapter(this);
		myConcertDbAdapter.open();
		Cursor results;
		if (!Utils.isEmpty(searchWord.getText().toString())) {
			results = myConcertDbAdapter.getSearchWordList(genre, searchWord
					.getText().toString());
		} else {
			if (genre == 0) {

				results = myConcertDbAdapter.fetchAllMyConcert();
			} else {

				results = myConcertDbAdapter.genreConcerList(genre);

			}
		}
		while (results.moveToNext()) {
			ConcertData concertData = new ConcertData();

			int id = results.getInt(0);
			int cultCode = results.getInt(1);
			int genreCode = results.getInt(2);
			String subjName = results.getString(3);
			String title = results.getString(4);
			String startDate = results.getString(5);
			String endDate = results.getString(6);
			String place = results.getString(7);
			String imagePath = results.getString(8);

			concertData.setId(id);
			concertData.setCultCode(cultCode);
			concertData.setGenreCode(genreCode);
			concertData.setTitle(title);
			concertData.setSubjName(subjName);
			concertData.setStartDate(startDate);
			concertData.setEndDate(endDate);
			concertData.setPlace(place);
			concertData.setImagepath(imagePath);

			myConcertDataArrayList.add(concertData);
			mCultCode.add(cultCode);

		}
		results.close();
		myConcertDbAdapter.close();
		mSubjCodeArtListAdapter = new SubjCodeArtListAdapter(this,
				myConcertDataArrayList);
		listView.setAdapter(mSubjCodeArtListAdapter);
	}

	OnItemLongClickListener mItemLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> arg0, View v,
				final int pos, long id) {

			Dialog sendDialog = new SendDialog(MyConcertListActivity.this,
					MyConcertListActivity.this, myConcertDataArrayList.get(pos));
			sendDialog.show();
			return false;
		}

	};

	protected void onLongListItemClick(int rowId) {

		myConcertDbAdapter = new MyConcertDbAdapter(this);
		myConcertDbAdapter.open();
		myConcertDbAdapter.deleteMyConcert(rowId);
		myConcertDbAdapter.close();
		myConcertDataArrayList.clear();
		loadGenreConcertList(genrePosition);

	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Dialog dialog = new AppCloseDialog(this, this);
			dialog.show();

		}
		return true;
	}

}
